package com.sicnu.cheer.leaugebar.contacts.adapter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sicnu.cheer.generalmodule.util.StringUtils;
import com.sicnu.cheer.leaugebar.contacts.R;
import com.sicnu.cheer.leaugebar.contacts.utils.SortModel;

import java.util.List;

public class SortAdapter extends BaseAdapter implements SectionIndexer {
    private List<SortModel> list = null;
    private Activity mContext;
    private View.OnClickListener callPhoneListener;

    public SortAdapter(Activity context, List<SortModel> list) {
        this.mContext = context;
        this.list = list;

        callPhoneListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + String.valueOf(v.getTag().toString())));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        };
    }

    final static class ViewHolder {
        TextView tvLetter;
        ImageView userHead;
        TextView tvTitle;
        TextView depName;
        ImageView phoneImage;
        TextView phoneNum;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<SortModel> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public int getCount() {
        return this.list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup arg2) {
        ViewHolder viewHolder = null;
        final SortModel mContent = list.get(position);
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_sort_listview, null);
            viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
            viewHolder.userHead = (ImageView) view.findViewById(R.id.user_head);
            viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
            viewHolder.depName = (TextView) view.findViewById(R.id.dep_name);
            viewHolder.phoneImage = (ImageView) view.findViewById(R.id.phone_img);
            viewHolder.phoneNum = (TextView) view.findViewById(R.id.phone_number);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(mContent.getSortLetters());
        } else {
            viewHolder.tvLetter.setVisibility(View.GONE);
        }

        SortModel sortModel = this.list.get(position);
        if (sortModel != null) {
            //设置头像
            Glide.with(mContext).load(sortModel.getHeadUrl()).placeholder(R.mipmap.default_user_img).into((viewHolder.userHead));
            //设置 姓名
            String name = sortModel.getUsers().getNick() == null ? "" : sortModel.getUsers().getNick();
            viewHolder.tvTitle.setText(name);
            //设置部门名称
            String depName = sortModel.getUsers().getMajor() == null ? "" : sortModel.getUsers().getMajor();
            viewHolder.depName.setText(depName);
            //设置电话号码
            String phone = sortModel.getUsers().getMobilePhoneNumber() == null ? "" : sortModel.getUsers().getMobilePhoneNumber();
            if (StringUtils.isEmpty(phone)) {
                viewHolder.phoneNum.setVisibility(View.GONE);
                viewHolder.phoneImage.setVisibility(View.GONE);
            } else {
                viewHolder.phoneNum.setVisibility(View.VISIBLE);
                viewHolder.phoneImage.setVisibility(View.VISIBLE);
                viewHolder.phoneNum.setText(phone);
                //设置打电话监听
                viewHolder.phoneImage.setTag(viewHolder.phoneNum.getText().toString());
                viewHolder.phoneNum.setTag(viewHolder.phoneNum.getText().toString());
                if (!StringUtils.isEmpty(phone)) {
                    viewHolder.phoneNum.setOnClickListener(callPhoneListener);
                    viewHolder.phoneImage.setOnClickListener(callPhoneListener);
                }
            }
        }

        return view;
    }


    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    public interface UserCheckBoxClickBack {
        void click(View v);
    }
}